package com.projecthost.wizaz;

import java.util.List;

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.projecthost.util.HtmlParser;
import com.projecthost.wizaz.model.ProductBase;
import com.projecthost.wizaz.model.ProductDetailed;

public class WizazProductCrawler {

    private final HtmlParser htmlParser;
    private final String mainWizazUrl;

    public WizazProductCrawler(HtmlParser htmlParser, String mainWizazUrl) {
        this.htmlParser = htmlParser;
        this.mainWizazUrl = mainWizazUrl;
    }

    public ProductDetailed getProductDetailed(int productId) {
        String productUrl = ProductBase.getProductUrl(productId, 1);
        HtmlPage productPage = htmlParser.getHtmlPageFromUrl(mainWizazUrl + productUrl, false);
        HtmlDivision contentDiv = (HtmlDivision) productPage.getElementById("content");
        String productName = contentDiv.getElementsByTagName("h1").get(0).getTextContent();
        HtmlDivision detailsDiv = (HtmlDivision) productPage.getElementById("details");
        HtmlAnchor categoryHref = (HtmlAnchor) detailsDiv.getByXPath(".//ul/li/strong[text()='Kategoria:']/../a").get(0);
        HtmlAnchor brandAHref = (HtmlAnchor) detailsDiv.getByXPath(".//ul/li/strong[text()='Marka:']/../a").get(0);

        Integer myHitCount = 0;
        @SuppressWarnings("unchecked")
        List<HtmlSpan> myHit = (List<HtmlSpan>) detailsDiv.getByXPath(".//ul[@class='stamps']//span");
        if (myHit.size() > 0) {
            myHitCount = Integer.valueOf(myHit.get(0).getTextContent());
        }

        HtmlSpan averageSpan = (HtmlSpan) detailsDiv.getByXPath(".//ul/li[@class='average']/span").get(0);
        String averageSpanText = averageSpan.getTextContent();
        String ratingText = averageSpanText.substring(1, averageSpanText.indexOf("/"));
        Float rating = Float.valueOf(ratingText);
        Integer reviewCount = Integer.valueOf(averageSpanText.substring(averageSpanText.indexOf(" z ") + 3,
                averageSpanText.lastIndexOf(" ")));

        @SuppressWarnings("unchecked")
        List<HtmlListItem> starsDistributionItems = (List<HtmlListItem>) detailsDiv
                .getByXPath(".//ul/li[@class='charts']/div[1]/ul[@class='labels']/li");
        Integer[] starsDistribution = new Integer[5];
        for (HtmlListItem starValue : starsDistributionItems) {
            Integer starNumber = Integer.valueOf(starValue.getAttribute("class").substring("label-".length()));
            String starPercentage = starValue.getTextContent();
            starsDistribution[starNumber - 1] = Integer.valueOf(starPercentage.substring(0, starPercentage.length() - 1));
        }

        HtmlDivision descriptionDiv = (HtmlDivision) productPage.getElementById("desc");
        String description = descriptionDiv.getTextContent().trim();

        return new ProductDetailed(productId, productName, brandAHref.getTextContent(), categoryHref.getHrefAttribute(),
                description, myHitCount, rating, reviewCount, starsDistribution);
    }

}
